package com.ericcson.innerportalapp;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Paint;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ToDoListArrayAdapter extends ArrayAdapter<ToDoItem> {
	
	private int resource;
	private Context context;
	private ActionMode actionMode;
	private OnEditItemSelectListener onEditItemSelectListener;
	
	public void setOnEditItemSelectListener(
			OnEditItemSelectListener onEditItemSelectListener) {
		this.onEditItemSelectListener = onEditItemSelectListener;
	}

	public interface OnEditItemSelectListener{
		public void onEditItemSelected();
	}

	public ToDoListArrayAdapter(Context context, int resource,
			 List<ToDoItem> objects) {
		super(context, resource, objects);
		
		this.resource = resource;
		this.context = context;
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		ToDoItem toDoItem = getItem(position);
		LayoutInflater layoutInflator = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		View listItemView = layoutInflator.inflate(resource, null);
		
		final TextView textView = (TextView) listItemView.findViewById(R.id.todo_list_item);
		CheckBox checkBox = (CheckBox) listItemView.findViewById(R.id.todo_checkbox);
		
		textView.setTag(toDoItem);
		
		checkBox.setChecked(toDoItem.isChecked());
		checkBox.setTag(toDoItem);
		
		
		textView.setText(toDoItem.getTask());
		
		textView.setTextColor(getContext().getResources().getColor(android.R.color.holo_blue_dark));
		
		if(toDoItem.isChecked()){
			textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
		}
		else {
			
			textView.setPaintFlags(textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
		}
		
		checkBox.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CheckBox cb = (CheckBox) v;
				ToDoItem toDoItem = (ToDoItem) cb.getTag();
				toDoItem.setChecked(cb.isChecked());
				
				if(toDoItem.isChecked()){
					textView.setPaintFlags(textView.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
				}
				else {
					
					textView.setPaintFlags(textView.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
				}
			}
		});
		
		textView.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(final View view) {
				if (actionMode != null) {
		            return false;
		        }
				
				actionMode = ((ToDoListActivity) context).startActionMode(new ActionMode.Callback() {

					@Override
					public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
						switch (item.getItemId()) {
			            case R.id.context_delete_todoitem:
			            	((ToDoListActivity) context).deleteTodoItem(position);
			                mode.finish();
			                return true;
			                
						}
						return false;
					}

					@Override
					public boolean onCreateActionMode(ActionMode mode, Menu menu) {
						MenuInflater inflater = mode.getMenuInflater();
				        inflater.inflate(R.menu.context_menu, menu);
						return true;
					}

					@Override
					public void onDestroyActionMode(ActionMode mode) {
						actionMode = null;
						
					}

					@Override
					public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
						// TODO Auto-generated method stub
						return false;
					}
					
					
				});
		        view.setSelected(true);
		        return true;
				
			}
		});
		
		textView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				TextView tv = (TextView) v;
				ToDoItem toDoItem = (ToDoItem) tv.getTag();
				
				EditText editText = (EditText) ((ToDoListActivity) getContext()).findViewById(R.id.new_todo);
				editText.setText(toDoItem.getTask());
				editText.requestFocus();
				InputMethodManager im = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
				im.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
				editText.setTag(R.string.position, position);
				
				onEditItemSelectListener.onEditItemSelected();
			}
		});
		
		return listItemView;
		
	}

}
